package autoteam;

import java.util.HashMap;
import java.util.Map;

import db.ColumnData;
import db.DataAccessObject;
import db.SQLDataTypes;
import db.TableField;

public class PlayerData
{
	
	DataAccessObject dao = new DataAccessObject();
	
	TableField lastName		= new TableField("lastname", SQLDataTypes.VARCHAR);
	TableField firstName	= new TableField("firstname", SQLDataTypes.VARCHAR);
	TableField playerID		= new TableField("playerid", SQLDataTypes.INTEGER);
	TableField phone		= new TableField("phone", SQLDataTypes.VARCHAR);
	TableField email		= new TableField("email", SQLDataTypes.VARCHAR);
	TableField height		= new TableField("height", SQLDataTypes.INTEGER);
	TableField weight		= new TableField("weight", SQLDataTypes.INTEGER);
	TableField experience	= new TableField("experience", SQLDataTypes.INTEGER);
	TableField monday 		= new TableField("monday", SQLDataTypes.INTEGER);
	TableField tuesday 		= new TableField("tuesday", SQLDataTypes.INTEGER);
	TableField wednesday	= new TableField("wednesday", SQLDataTypes.INTEGER);
	TableField thursday		= new TableField("thursday", SQLDataTypes.INTEGER);
	TableField friday 		= new TableField("friday", SQLDataTypes.INTEGER);
	TableField skill		= new TableField("skill", SQLDataTypes.VARCHAR);
	TableField comment		= new TableField("comment", SQLDataTypes.VARCHAR);
	
	String[] skillField;
	
	public PlayerData()
	{
		
	}
	
	public PlayerData(String[] skillNames)
	{
		dao.connect();
		
		skillField = skillNames;
		//dao.createTable("players", lastName, firstName, playerID, phone, email, height, weight, 
		//		experience, monday, tuesday, wednesday, thursday, friday, skill, comment);
		
		//dao.disconnect();
	}
	
	public void addPlayer(Child c)
	{
		dao.connect();
		
		String tempSkill = "";
		for (int i = 0; i < skillField.length; i++)
		{
			if (i == 0)
				tempSkill += skillField[i] + c.skill[i];
			else
				tempSkill += ", " + skillField[i] + c.skill[i];
		}
		
		ColumnData[] identifier = new ColumnData[15];
		
		identifier[0] = new ColumnData("lastname", c.lastName);
		identifier[1] = new ColumnData("firstname", c.firstName);
		identifier[2] = new ColumnData("playerid", c.id);
		identifier[3] = new ColumnData("phone", c.phone);
		identifier[4] = new ColumnData("email", c.email);
		identifier[5] = new ColumnData("height", c.height);
		identifier[6] = new ColumnData("weight", c.mass);
		identifier[7] = new ColumnData("experience", c.experience);
		Map<Boolean,Integer> booleanToInt = new HashMap<Boolean,Integer>();
		booleanToInt.put(true, 1);
		booleanToInt.put(false, 0);
		identifier[8] = new ColumnData("monday", booleanToInt.get(c.getBaseNight(0)));
		identifier[9] = new ColumnData("tuesday", booleanToInt.get(c.getBaseNight(1)));
		identifier[10] = new ColumnData("wednesday", booleanToInt.get(c.getBaseNight(2)));
		identifier[11] = new ColumnData("thursday", booleanToInt.get(c.getBaseNight(3)));
		identifier[12] = new ColumnData("friday", booleanToInt.get(c.getBaseNight(4)));
		identifier[13] = new ColumnData("skill", tempSkill);
		identifier[14] = new ColumnData("comment", c.comment);
		
		dao.addRecord("players", identifier);
		
		dao.disconnect();
	}
	
	public void deletePlayer(int playerId)
    {
        dao.connect();
        ColumnData idCol = new ColumnData("playerid",playerId);
        dao.deleteRecord("players", idCol);
        dao.disconnect();   	
    }
	
	public void updateFirstName(int playerId, String newName)
	{
		dao.connect();
		ColumnData[] identifier = new ColumnData[1];
		identifier[0] = new ColumnData("playerid", playerId);
		ColumnData[] change = new ColumnData[1];
		change[0] = new ColumnData("firstname", newName);
		dao.updateRecord("players", identifier, change);
	}
	
	public void updateLastName(int playerId, String newName)
	{
		dao.connect();
		ColumnData[] identifier = new ColumnData[1];
		identifier[0] = new ColumnData("playerid", playerId);
		ColumnData[] change = new ColumnData[1];
		change[0] = new ColumnData("lastname", newName);
		dao.updateRecord("player", identifier, change);
	}
	
}
